// alliance//merchantList/index.js
const utils = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantList: [],
    allianceId: 0,
    keyword: '',
    CurrentPage: 1,
    loadingMoreHidden: 1
  },

  getMerchantList: function() {
    let that = this;
    let keyword = that.data.keyword;
    let currentPage = that.data.CurrentPage;    
    let location = wx.getStorageSync('location');    
    let lat = location.lat;
    let log = location.lng;
    if (!lat) {
      lat = 0;
    }
    if (!log) {
      log = 0;
    }
    wx.request({
      url: app.globalData.host + '/api/Alliance/GetNearbyStaffList?uid=' + app.globalData.userInfo.Uid + '&keyword=' + keyword + '&page=' + currentPage + '&latitude=' + lat + '&longitude=' + log,
      success: function(res) {
        if (res.data.Code == 0) {
          let merList = res.data.Data.Items;
          if (currentPage != 1) {
            var preGoods = that.data.merchantList
            merList = preGoods.concat(merList);
          }
          for (let i = 0; i < merList.length; i++) {
            merList[i].Avater = utils.formatAvater(merList[i].Avater, 'https://static.711688.net.cn/upload/card/source/');
            merList[i].Distance = parseInt(merList[i].Distance);
            if (merList[i].Distance >= 1000){
              merList[i].Distances = parseFloat(merList[i].Distance / 1000).toFixed(2);
            }
          }
          that.setData({
            merchantList: merList,
            loadingMoreHidden: res.data.Data.TotalPages
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },
  // 邀请
  invate: function(e) {
    let that = this;
    let uid = e.currentTarget.dataset.uid;
    let allianceId = that.data.allianceId;
    wx.request({
      url: app.globalData.host + '/api/Alliance/AddApply?allianceId=' + allianceId + '&uid=' + uid,
      success: function(res) {
        if (res.data.Code == 0) {
          wx.showToast({
            title: '发送邀请成功',
            duration: 2000
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.allianceId) {
      this.setData({
        allianceId: options.allianceId
      })
    }
    this.getLocation();
  },
  // 加载更多
  loadMore: function (e) {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getMerchantList();
    } else {
      return
    }
  },
  // 获取地理位置  
  getLocation: function () {
    let that = this;
    utils.getLocation(()=>{
      that.getMerchantList();
    },app);   
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  handleInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  handleBlur: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  handleConfirm: function (e) {
    this.setData({
      keyword: e.detail.value,
      CurrentPage: 1
    })
    this.getMerchantList();
  },
  handleSearchWord: function () {
    this.setData({
      CurrentPage: 1
    })
    this.getMerchantList();
  }
})